<template>
  <div class="contain">
    <!-- 位置的联级选择 -->
     <van-field
      class="field"
      clickable
      label="仓库"
      :value="house"
      placeholder="点击选择仓库"
      auto-focus=true
      @click="changeshowPicker"
    />
    <van-popup :show="showPicker" round position="bottom" overlay>
         <div class="picker">
        <van-picker
          show-toolbar
          title="仓库选择"
          :columns="houseList"
          @cancel="showPicker = false"
          @confirm="onConfirm"
          @change="onChange"
        />
      </div>
    </van-popup>

    <van-field
      class="field"
      clickable
      label="货架"
      :value="address"
      @change="addchange"
      placeholder="请输入货架"
    />
    <van-button class="btn" icon="scan" type="primary" @click="toscancode">进入扫码清点</van-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 仓库值
      house: "",
      address: "",
      // 仓库的选择框
      showPicker: true,
      // 仓库选择
      houseList: [],
      idarr: [],
      showPicker: false
    };
  },
  methods: {
    // 点击弹出层确认
    onConfirm(value) {
      // console.log(this.columns[0]);
      let value1 = value.mp.detail.value;
      this.house = value1;
      this.showPicker = false;
      // console.log(this.add);
    },
    onChange(even) {
      console.log(even);
    },
    // 地址填写
    addchange(even) {
      this.address = even.mp.detail;
    },
    async changeshowPicker() {
      const res = await this.$http.get({ url: "crms/crmsCheck/getHouse" });
      this.houseList = res.data.houseList;
      console.log(this.houseList);
      wx.hideKeyboard({
        complete: res => {
          this.showPicker = true;
        }
      });
    },
    toscancode() {
      wx.scanCode({
        onlyFromCamera: false,
        scanType: ["qrCode", "barCode", "datamatrix", "pdf417"],
        success: async result => {
          console.log(this.address);
          var str = result.result.lastIndexOf("/");
          var id = result.result.substring(str + 1, result.result.length);
          const res = await this.$http.get({
            url:
              "crms/crmsCheck/isUpdateCheck?crAddr=" +
              this.address +
              "&crNo=" +
              id
          });
          if (res.data.isPosition == false) {
            this.idarr.push(id);
            console.log(this.idarr);
            wx.showToast({
              title: "未清点",
              icon: "none"
            });
          }
          this.toscancode();
        },
        fail: async err => {
          const res = await this.$http.post({
            url: "crms/crmsCheck/record?crNoList=" + this.idarr
          });
          console.log(res);
          wx.navigateTo({
            url: "/pages/errmsg/main?idarr=" + this.idarr
          });
          wx.showToast({
            title: "跳转至异常表",
            icon: "none"
          });
        }
      });
    }
  },
  beforeMount() {
      this.changeshowPicker()
  },
};
</script>

<style>
page {
  height: 100%;
  background: #f3f3f3;
}
.contain {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.field {
  margin-bottom: 100rpx;
}
.btn {
  margin-top: 20rpx;
}
</style>